package com.shelpe.services.sinbad.controller.actionLog.impl.campaign;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.controller.actionLog.ActionLogDto;
import com.shelpe.services.sinbad.controller.actionLog.ActionLogService;
import com.shelpe.services.sinbad.controller.actionLog.CampaignVo;
import com.shelpe.services.sinbad.entity.jpa.AreaList;
import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.entity.jpa.CampaignLog;

@Service
public class CampaignSemiautoConverter extends ActionLogService {

	@Override
	public ActionLogDto convertCampaign(CampaignLog entity, Map<Long, Campaign> campaignMap, List<AreaList> areas) {
		boolean campaignSemiautoConverter = entity.getAction() == CampaignLog.Action.SEMIAUTO_EXEC_DELETE
				|| entity.getAction() == CampaignLog.Action.SEMIAUTO_EXEC_MATCH
				|| entity.getAction() == CampaignLog.Action.SEMIAUTO_EXEC_PRICING
				|| entity.getAction() == CampaignLog.Action.SEMIAUTO_EXEC_PRICING_MATCH;
		if (campaignSemiautoConverter) {
			if (StringUtils.isNotBlank(entity.getNote())) {
				ActionLogDto dto = new ActionLogDto();
				dto.from(entity);
				Campaign campaign = campaignMap.get(entity.getCampaignID());
				CampaignVo vo = this.unserializeCampaign(entity.getNote());
				if (campaign != null && vo != null && StringUtils.isNotBlank(vo.getSemiautoMessage())) {
					dto.setReason(vo.getSemiautoMessage());
					return dto;
				}
			}
		}
		return null;
	}

}
